package com.alibaba.yinan.word;

import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @author yinan
 */
@RestController
@RequestMapping("/word")
public class WordGenController {

    @Resource
    private PoiTlTemplateGenerator poiTlTemplateGenerator;

    @Resource
    private WordToJsonExample wordToJsonExample;

    @PostMapping("/generate")
    public void generateReport() throws Exception {
        // 从 classpath 加载模板文件（打包后也能用）
        try (InputStream templateStream = getClass().getClassLoader()
                .getResourceAsStream("templates/word-template.docx")) {

            if (templateStream == null) {
                throw new FileNotFoundException("未找到模板文件 templates/word-template.docx！");
            }

            String outputPath = "output/report.docx";
            poiTlTemplateGenerator.generateReport(templateStream, outputPath);
        }
    }


    @PostMapping("/readReport")
    public JSONObject readReport() throws Exception {
        return wordToJsonExample.parseWordToJson("/Volumes/External_1TB/Projects/monco/yinan-task-service/start/src/main/resources/templates/XRD-import-report.docx");
    }
}
